package platform_api.Controller;

import platform_api.Model.JsonMessage;
import org.springframework.web.bind.annotation.*;
import platform_api.Service.WeatherService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/weather")
public class WeatherDataRestController extends Controller {

    private final WeatherService weatherService;

    public WeatherDataRestController(WeatherService weatherService) {
        this.weatherService = weatherService;
    }

    /**
     * 获取气象数据
     *
     * @param sTime    开始时间
     * @param eTime    结束时间
     * @param city     城市
     * @param column   字段
     * @param distance 间隔
     * @return json数据
     * @throws ParseException 转换错误
     */
    @ResponseBody
    @GetMapping("/get")
    public JsonMessage getData(String sTime, String eTime, String city, String column, String distance) throws ParseException {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        Date startTime, endTime;
        startTime = df.parse(getRequestOrDefault(sTime, "2011-1-1"));
        endTime = df.parse(getRequestOrDefault(eTime, "2011-2-1"));
        city = getRequestOrDefault(city, "1");
        column = getRequestOrDefault(column, "T");
        distance = getRequestOrDefault(distance, "hour");

        return weatherService.getChartData(city, column, startTime, endTime, distance);
    }

    /**
     * 获取城市列表
     *
     * @return JsonMessage
     */
    @ResponseBody
    @GetMapping("/city_list")
    public JsonMessage getCityList(){
        return weatherService.getCityInfo();
    }
}
